package com.elitedatai.mchtest.map.repository;

import com.elitedatai.mchtest.map.model.entity.FlightAreaFileEntity;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface FlightAreaFileRepository extends JpaRepository<FlightAreaFileEntity, Long> {
    @Query("select f from FlightAreaFileEntity f where f.workspaceId = :workspaceId and f.latest = true")
    FlightAreaFileEntity findByWorkspaceIdAndLatestTrue(String workspaceId);


    // 根据 workspaceId 和 latest = true 查询，按 updateTime 降序，取第一条
    @Query("SELECT f FROM FlightAreaFileEntity f " +
            "WHERE f.workspaceId = :workspaceId " +
            "AND f.latest = true " +
            "ORDER BY f.updateTime DESC")
    Optional<FlightAreaFileEntity> findLatestByWorkspaceId(@Param("workspaceId") String workspaceId);

    /**
     * 根据 fileId 查询唯一记录（自动 limit 1）
     */
    Optional<FlightAreaFileEntity> findByFileId(String fileId);
}
